# Maintainer: George Rawlinson <george@rawlinson.net.nz>

pkgname=bupstash
pkgver=0.12.0
pkgrel=2
pkgdesc='A tool for encrypted backups'
arch=('loong64' 'x86_64')
url='https://bupstash.io'
license=('MIT')
depends=('libsodium' 'gcc-libs')
makedepends=('git' 'rust' 'ruby-ronn-ng')
checkdepends=('bash-bats' 'python' 'jq') # if you hate yourself, add procps-ng + bubblewrap
options=('!lto')
_commit='edab7c3db03e20e557272ee6999ede216b7693d2'
source=("$pkgname::git+https://github.com/andrewchambers/bupstash.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # download dependencies
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}
build() {
  cd "$pkgname"

  cargo build --release --frozen
}

check() {
  cd "$pkgname"

  # run cargo tests
  cargo test

  # run cli tests
  # ensure recently-built binary is first in $PATH
  PATH="$(pwd)/target/release:$PATH"
  bats ./cli-tests
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" "target/release/$pkgname"

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE

  # documentation
  install -vd "$pkgdir/usr/share/doc/$pkgname"
  cp -vr doc/* "$pkgdir/usr/share/doc/$pkgname"
  rm -vrf "$pkgdir/usr/share/doc/$pkgname/man"

  # man pages
  OLDIFS="$IFS"
  IFS="."
  cd doc/man
  for filename in *; do
    read -ra PART <<< "$filename"

    title="${PART[0]}"
    section="${PART[1]}"

    # generate man directory if required
    if [ ! -d "$pkgdir/usr/share/man/man${section}" ]; then
      install -vd "$pkgdir/usr/share/man/man${section}"
    fi

    # generate man page
    ronn --roff \
      <"$filename" \
      >"$pkgdir/usr/share/man/man${section}/${title}.${section}"
  done
  IFS="$OLDIFS"
  unset OLDIFS
}
